/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package net.tygzs.minerpg.Configuration.pvp;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import net.tygzs.minerpg.Main;
import net.tygzs.minerpg.lib.MathLib;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player;

/**
 *
 * @author thtTNT
 */
public class PVPConfig
{

    private final Map<String, Integer> kill = new HashMap<>();
    private final Map<String, Integer> killed = new HashMap<>();
    private final Map<RankType, Integer> demand = new HashMap<>();
    private final Map<String, RankType> playerRank = new HashMap<>();
    private final Map<String, Integer> kingRank = new HashMap<>();

    public PVPConfig()
    {
        load();
    }

    private void load()
    {
        try
        {
            File file = new File(Main.getPlugin().getDataFolder(), "PVP");
            if (!file.exists())
            {
                file.mkdirs();
            }
            file = new File(file, "data.yml");
            if (!file.exists())
            {
                file.createNewFile();
            }
            FileConfiguration config = YamlConfiguration.loadConfiguration(file);
            Set<String> names = config.getKeys(false);
            for (String name : names)
            {
                kill.put(name, config.getInt(name + ".kill"));
                killed.put(name, config.getInt(name + ".killed"));
            }
            if (Main.getPlugin().getConfig().contains("rank.demand.bronze"))
            {
                this.demand.put(RankType.bronze, Main.getPlugin().getConfig().getInt("rank.demand.bronze"));
            } else
            {
                this.demand.put(RankType.bronze, 25);
            }
            if (Main.getPlugin().getConfig().contains("rank.demand.silver"))
            {
                this.demand.put(RankType.silver, Main.getPlugin().getConfig().getInt("rank.demand.silver"));
            } else
            {
                this.demand.put(RankType.silver, 100);
            }
            if (Main.getPlugin().getConfig().contains("rank.demand.gold"))
            {
                this.demand.put(RankType.gold, Main.getPlugin().getConfig().getInt("rank.demand.gold"));
            } else
            {
                this.demand.put(RankType.gold, 300);
            }
            if (Main.getPlugin().getConfig().contains("rank.demand.platinum"))
            {
                this.demand.put(RankType.platinum, Main.getPlugin().getConfig().getInt("rank.demand.platinum"));
            } else
            {
                this.demand.put(RankType.platinum, 1000);
            }
            if (Main.getPlugin().getConfig().contains("rank.demand.diamond"))
            {
                this.demand.put(RankType.diamond, Main.getPlugin().getConfig().getInt("rank.demand.diamond"));
            } else
            {
                this.demand.put(RankType.diamond, 3000);
            }
        } catch (IOException ex)
        {
            Logger.getLogger(PVPConfig.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void reload()
    {
        load();
    }

    public void save()
    {
        try
        {
            File file = new File(Main.getPlugin().getDataFolder(), "PVP/data.yml");
            file.delete();
            file.createNewFile();
            FileConfiguration config = YamlConfiguration.loadConfiguration(file);
            Set<String> names = kill.keySet();
            for (String name : names)
            {
                config.set(name + ".kill", kill.get(name));
            }
            names = killed.keySet();
            for (String name : names)
            {
                config.set(name + ".killed", killed.get(name));
            }
            config.save(file);
        } catch (IOException ex)
        {
            Logger.getLogger(PVPConfig.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void PlayerKilled(String player, String killer)
    {
        if (killed.containsKey(player))
        {
            int AmountKilled = killed.get(player);
            killed.put(player, AmountKilled + 1);
        } else
        {
            killed.put(player, 1);
        }
        if (killer == null)
        {
            return;
        }
        if (kill.containsKey(killer))
        {
            int AmountKill = kill.get(player);
            kill.put(player, AmountKill + 1);
        } else
        {
            kill.put(killer, 1);
        }
    }

    public int getKill(String name)
    {
        return this.kill.get(name);
    }

    public int getKilled(String name)
    {
        return this.killed.get(name);
    }

    public void updatarank()
    {
        Bukkit.getScheduler().runTaskAsynchronously(Main.getPlugin(), new Runnable()
        {

            @Override
            public void run()
            {
                Bukkit.broadcastMessage(ChatColor.GREEN + "排位系统开始全服排序!");
                playerRank.clear();
                List<String> rank = new ArrayList<>();
                Map<String, Integer> soccers = new HashMap<>();
                for (String playerName : kill.keySet())
                {
                    int kill = Main.getPluginConfig().getPVPConfig().kill.get(playerName);
                    int killed = Main.getPluginConfig().getPVPConfig().killed.get(playerName);
                    int soccer = (int) ((kill / (killed + 1) - 0.1) * kill);
                    soccers.put(playerName, soccer);
                }
                List<Map.Entry<String, Integer>> rankList = MathLib.rankMapUp(soccers);
                int index = 0;
                for (int i = index; i <= rankList.size(); i++)
                {
                    if (rankList.get(i).getValue() > demand.get(RankType.bronze))
                    {
                        index = i;
                        break;
                    }
                    if (i == rankList.size())
                    {
                        sendResault();
                    }
                }
                for (int i = index; i <= rankList.size(); i++)
                {
                    if (rankList.get(i).getValue() > demand.get(RankType.silver))
                    {
                        index = i;
                        break;
                    }
                    playerRank.put(rankList.get(i).getKey(), RankType.bronze);
                    if (i == rankList.size())
                    {
                        sendResault();
                    }
                }
                for (int i = index; i <= rankList.size(); i++)
                {
                    if (rankList.get(i).getValue() > demand.get(RankType.gold))
                    {
                        index = i;
                        break;
                    }
                    playerRank.put(rankList.get(i).getKey(), RankType.silver);
                    if (i == rankList.size())
                    {
                        sendResault();
                    }
                }
                for (int i = index; i <= rankList.size(); i++)
                {
                    if (rankList.get(i).getValue() > demand.get(RankType.platinum))
                    {
                        index = i;
                        break;
                    }
                    playerRank.put(rankList.get(i).getKey(), RankType.gold);
                    if (i == rankList.size())
                    {
                        sendResault();
                    }
                }
                for (int i = index; i <= rankList.size(); i++)
                {
                    if (rankList.get(i).getValue() > demand.get(RankType.diamond))
                    {
                        index = i;
                        break;
                    }
                    playerRank.put(rankList.get(i).getKey(), RankType.platinum);
                    if (i == rankList.size())
                    {
                        sendResault();
                    }
                }
                for (int i = index; i <= rankList.size(); i++)
                {
                    playerRank.put(rankList.get(i).getKey(), RankType.king);
                    kingRank.put(rankList.get(i).getKey(), rankList.size() - i);
                }
                sendResault();
            }

            private void sendResault()
            {
                for (Player player : Bukkit.getOnlinePlayers())
                {
                    if (!playerRank.containsKey(player.getName()))
                    {
                        player.sendMessage(ChatColor.GREEN + "很遗憾!您并没有进入段位!");
                        continue;
                    }
                    if (playerRank.get(player.getName()) == RankType.king)
                    {
                        player.sendMessage(ChatColor.GREEN + "恭喜您加入传说的行列!您现在的排名是" + kingRank.get(player.getName()));
                        continue;
                    }
                    player.sendMessage(ChatColor.GREEN + "您现在的段位是:" + RankType.getRankTypeChineseName(playerRank.get(player.getName())) + ",继续努力哦!");
                }
            }
        });
    }

}
